package com.pg.controller.kgg;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pg.entity.kgg.EipMaterialThickness;
import com.pg.service.EipMaterialThicknessService;
import com.pg.utils.PoiReadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 雷柜体材质、厚度前端控制器
 * </p>
 *
 * @author likl
 * @since 2020-07-06
 */
@Api(tags = "柜体材质、厚度")
@RestController
@RequestMapping("/eipMaterialThickness")
@Slf4j
public class EipMaterialThicknessController extends JeecgController<EipMaterialThickness, EipMaterialThicknessService> {

    @Autowired
    private EipMaterialThicknessService eipMaterialThicknessService;

    /**
     * 添加 柜体材质、厚度
     *
     * @param eipMaterialThickness 柜体材质、厚度
     * @return 是否成功
     */
    @ApiOperation("添加 柜体材质、厚度")
    @AutoLog(value = "添加 柜体材质、厚度")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody EipMaterialThickness eipMaterialThickness) {
        eipMaterialThickness.setPuid(UUID.randomUUID().toString());
        eipMaterialThicknessService.save(eipMaterialThickness);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑 柜体材质、厚度
     *
     * @param eipMaterialThickness 柜体材质、厚度
     * @return 是否成功
     */
    @ApiOperation("编辑 柜体材质、厚度")
    @AutoLog(value = "编辑 柜体材质、厚度")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody EipMaterialThickness eipMaterialThickness) {
        eipMaterialThicknessService.updateById(eipMaterialThickness);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除 柜体材质、厚度
     *
     * @param id 全库唯一码
     * @return 是否成功
     */
    @ApiOperation("通过id删除 柜体材质、厚度")
    @AutoLog(value = "通过id删除 柜体材质、厚度")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        eipMaterialThicknessService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除 柜体材质、厚度
     *
     * @param ids 全库唯一码集合
     * @return 是否成功
     */
    @ApiOperation("批量删除 柜体材质、厚度")
    @AutoLog(value = "批量删除 柜体材质、厚度")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.eipMaterialThicknessService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询 柜体材质、厚度
     *
     * @param id 全库唯一码
     * @return 柜体材质、厚度数据
     */
    @ApiOperation("通过id查询 柜体材质、厚度")
    @AutoLog(value = "通过id查询 柜体材质、厚度")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        EipMaterialThickness eipMaterialThickness = eipMaterialThicknessService.getById(id);
        if (eipMaterialThickness == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(eipMaterialThickness);
    }

    /**
     * 分页列表查询 柜体材质、厚度
     *
     * @param eipMaterialThickness 柜体材质、厚度
     * @param pageNo               起始页
     * @param pageSize             每页页数
     * @param req                  参数
     * @return 分页数据
     */
    @ApiOperation("分页列表查询 柜体材质、厚度")
    @AutoLog(value = "分页列表查询 柜体材质、厚度")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(EipMaterialThickness eipMaterialThickness,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<EipMaterialThickness> queryWrapper = QueryGenerator.initQueryWrapper(eipMaterialThickness, req.getParameterMap());
        Page<EipMaterialThickness> page = new Page<EipMaterialThickness>(pageNo, pageSize);
        IPage<EipMaterialThickness> pageList = eipMaterialThicknessService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 通过excel导入数据 柜体材质、厚度
     *
     * @param request  参数
     * @param response 响应
     * @return 是否成功
     */
    @ApiOperation("通过excel导入数据 柜体材质、厚度")
    @AutoLog(value = "通过excel导入数据 柜体材质、厚度")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();
            String fileName = file.getOriginalFilename();
            String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
            if (!PoiReadUtil.checkExtension(extension)){
                return Result.error("文件导入失败:文件格式错误");
            }
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<EipMaterialThickness> list = ExcelImportUtil.importExcel(file.getInputStream(), EipMaterialThickness.class, params);
                for (EipMaterialThickness eipMaterialThickness : list) {
                    eipMaterialThickness.setIoflag(eipMaterialThickness.getIoflag() == null ? "1" : eipMaterialThickness.getIoflag());
                    eipMaterialThicknessService.saveOne(eipMaterialThickness);
                }
                //update-begin-author:taoyan date:20190528 for:批量插入数据
                long start = System.currentTimeMillis();

                //400条 saveBatch消耗时间1592毫秒  循环插入消耗时间1947毫秒
                //1200条  saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
                //update-end-author:taoyan date:20190528 for:批量插入数据
                return Result.ok("文件导入成功！数据行数：" + list.size());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                //                return Result.error("文件导入失败:" + e.getMessage());
                return Result.error("文件导入失败:请检查excel模板是否正确");
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    /**
     * 导出excel 柜体材质、厚度
     *
     * @param request              参数
     * @param eipMaterialThickness 柜体材质、厚度
     */
    @ApiOperation("导出excel 柜体材质、厚度")
    @AutoLog(value = "导出excel 柜体材质、厚度")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, EipMaterialThickness eipMaterialThickness) {
        return super.exportXls(request, eipMaterialThickness, EipMaterialThickness.class, "eip_material_thickness");
    }
}
